package com.cls.system.mapper;

import com.cls.system.entity.SysDict;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 字典表 Mapper
 *
 * @author slx
 * @date 2020-11-09 15:11:51
 */
@Repository
public interface SysDictMapper extends BaseMapper<SysDict> {

    /**
     * 根据code查询字典
     * @param code
     * @return
     */
    @Select("select dict_id,name,value,code,enabled,remark,sort,pid from sys_dict where is_delete=0 and code=#{code}")
    SysDict selectByCode(@Param("code") String code);



    /**
     * 根据父级的code查询子集
     *
     * @param code
     * @return
     */
    @Select("select dict_id,name,value,code,enabled,remark,sort,pid from sys_dict where  pid=(select dict_id from sys_dict where code=#{code} and is_delete=0) and is_delete=0 ORDER BY sort")
    List<SysDict> findByParentCode(@Param("code") String code);

    /**
     * 根据子级的code查询父集
     *
     * @param code
     * @return
     */
    @Select("select dict_id,name,value,code,enabled,remark,sort,pid from sys_dict where dict_id=(select pid from sys_dict where code=#{code} and is_delete=0) and is_delete=0")
    SysDict findByChildrenCode(@Param("code") String code);


    /**
     * 根据多个codes查询
     *
     * @param codes
     * @return
     */
    List<SysDict> selectByCodes(@Param("codes") List<String> codes);

    @Select("delete from sys_dict where dict_id=#{dictId}")
    void deleteForId(@Param("dictId")Long dictId);

    /**
     * 以逗号分隔的code  查询  返回所有的name
     * @param codes
     * @return
     */
    @Select("select GROUP_CONCAT(name) from sys_dict where FIND_IN_SET(code,#{codes}) GROUP BY pid ")
    String findNameByGroupConcat(String codes);
}
